<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>zyupload上传实例</title>
    <link rel="stylesheet" href="/static/layuimini/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuimini/lib/jq-module/zyupload/zyupload-1.0.0.min.css" media="all">
    <link rel="stylesheet" href="/static/layuimini/css/public.css" media="all">
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">标题</label>
        <div class="layui-input-block">
            <input type="text" value="<<<.article_info.Title>>>" name="title" lay-verify="required" lay-reqtext="标题不能为空" placeholder="请输入标题" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label required">简介</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入简介" name="introduction" class="layui-textarea"><<<.article_info.Introduction>>></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">图片url</label>
        <div class="layui-input-block">
            <input type="text" value="<<<.article_info.ImageUrl>>>" name="image_url" lay-verify="required" lay-reqtext="图片不能为空" placeholder="请输入图片" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">目录</label>
        <div class="layui-input-block">
            <select name="cat_id" lay-filter="role_id">
                <option value="0">请选择</option>
                <<<range $index, $elem := .article_cat_list>>>
                <<<if eq $elem.Id $.article_info.CatId >>>
                    <option selected value="<<<$elem.Id>>>"><<<$elem.CatName>>></option>
                <<<else>>>
                    <option value="<<<$elem.Id>>>"><<<$elem.CatName>>></option>
                <<<end>>>
                <<<end>>>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">标签</label>
        <div class="layui-input-block">
            <<<range $index, $elem := .article_tags_list>>>
                <<<if eq $elem.Checked true>>>
                <input type="checkbox" checked name="tags" value="<<<$elem.Id>>>" lay-skin="primary" title="<<<$elem.TagsName>>>">
                <<<else>>>
                 <input type="checkbox" name="tags" value="<<<$elem.Id>>>" lay-skin="primary" title="<<<$elem.TagsName>>>">
                <<<end>>>
            <<<end>>>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">首页推荐</label>
        <div class="layui-input-block">
            <input type="radio" name="home_recommend" <<<if eq .article_info.HomeRecommend 2 >>>checked=""<<<end>>> value="2" title="是">
            <input type="radio" name="home_recommend" <<<if eq .article_info.HomeRecommend 1 >>>checked=""<<<end>>> value="1" title="否">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">站长推荐</label>
        <div class="layui-input-block">
            <input type="radio" name="blogger_recommend" <<<if eq .article_info.BloggerRecommend 2 >>>checked=""<<<end>>> value="2" title="是">
            <input type="radio" name="blogger_recommend" <<<if eq .article_info.BloggerRecommend 1 >>>checked=""<<<end>>> value="1" title="否">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="0" <<<if eq .article_info.Status 0 >>>checked=""<<<end>>> title="启用">
            <input type="radio" name="status" value="1" <<<if eq .article_info.Status 1 >>>checked=""<<<end>>> title="停用">
        </div>
    </div>
    <div class="layui-form-item required">
        <label class="layui-form-label required">正文</label>
        <div class="layui-input-block">
            <div id="editor" style="margin: 50px 0 50px 0">

            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            <input type="hidden" name="id" value="<<<.article_info.Id>>>">
        </div>
    </div>
</div>
<script src="/static/layuimini/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="/static/layuimini/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/layuimini/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script type="text/javascript">
    $(function () {
        layui.use(['jquery','form','wangEditor'], function(){
            var $ = layui.jquery,
                form = layui.form;

            //监听提交
            form.on('submit(saveBtn)', function (data) {
                data = data.field
                var content = editor.txt.html();
                tags_arr = [];
                $('input[name=tags]:checked').each(function() {
                    tags_arr.push($(this).val());
                });
                var tags = tags_arr.join(',');
                $.ajax({
                    url: "/admin/ajax_edit_article",
                    data:{id:data.id,title:data.title,introduction:data.introduction,cat_id:data.cat_id,image_url:data.image_url,tags:tags,home_recommend:data.home_recommend,blogger_recommend:data.blogger_recommend,status:data.status,content:content},
                    type:"POST",
                    success: function(data){
                        layer.msg(data.msg);
                        if(data.code == 10000){
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);

                            parent.layui.table.reload('currentTableId',{page:{curr:1}}); //demo表格id
                        }

                    }
                });

                return false;
            });

            var content = "<<<.article_info.Content>>>";

            var E = window.wangEditor;
            var editor = new E('#editor');
            editor.customConfig.uploadImgServer = "/admin/ajax_upload_image";
            editor.customConfig.uploadFileName = 'image';
            editor.customConfig.pasteFilterStyle = false;
            editor.customConfig.uploadImgMaxLength = 5;
            editor.customConfig.uploadImgHooks = {
                // 上传超时
                timeout: function (xhr, editor) {
                    layer.msg('上传超时！')
                },
                // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式，可使用该配置
                customInsert: function (insertImg, result, editor) {
                    console.log(result);
                    if (result.code == 1) {
                        var url = result.data.url;
                        url.forEach(function (e) {
                            insertImg(e);
                        })
                    } else {
                        layer.msg(result.msg);
                    }
                }
            };
            editor.customConfig.customAlert = function (info) {
                layer.msg(info);
            };
            editor.create();
            editor.txt.html(content);
        });

    });
</script>

</body>
</html>